package com.iamk.tool;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import javax.swing.filechooser.FileNameExtensionFilter;

import net.miginfocom.swing.MigLayout;

import com.iamk.feature.FColor;
import com.iamk.util.Convert;
import com.iamk.util.ListFilesUtil;

public class AutoAnnotation extends JPanel implements ActionListener{

	private static final long serialVersionUID = -6379686852384264519L;

	final String[] LABEL = { 
							"building",
							"grass",
							"tree",
							"cow",
							"sheep",
							"sky",
							"mountain",
							"aeroplane",
							"water",
							"face",
							"car",
							"bicycle",
							"flower",
							"sign",
							"road",
							"body",
							"boat"};

	JButton btnOpenFolder;
	JButton btnClear;
	JButton btnSet;
	JButton btnWriteLevel1Data;

	JRadioButton rdbUseTrainingSet;
	JRadioButton rdbUseCrossValidation;

	JList<String> lsLabel;
	JScrollPane scrollLabel;
	
	JList<String> lsFileName;
	JScrollPane scrollFileName;
	
	JTextArea txtResult;
	JScrollPane scrollResult;

	JLabel lblPath;
	JLabel lblClassifer;
	JLabel lblFold;
	
	JLabel picImageOriginLabel;
	JLabel picImageSegmentLabel;

	JTextField tfPath;
	JTextField tfFold;

	JPanel mPanelImageOriginal;
	JPanel mPanelImageSegmentation;
	JPanel mPanelFileName;
	JPanel mPanelChooserLabel;
	JPanel mPanelLabelResult;
	JPanel mPanelManualLabel;
	JPanel mPanelAutoLabel;

	JScrollPane dataPane;
	JScrollPane outputPane;
	
	JList<String> lsManualLabel;
	JScrollPane manualLabelPane;
	
	JList<String> lsAutoLabel;
	JScrollPane autoLabelPane;

	JTextArea txtData;
	JTextArea txtOutput;

	JFileChooser dlgChooseFolder;

	String path;
	
	Image imgOriginal;
	Image imgSegmentation;
	FColor fImage;
	int regionSelected;
	
	ListFilesUtil lfu = new ListFilesUtil();
	public AutoAnnotation() {
		// TODO Auto-generated constructor stub
		super(null);
		setPropertiesComponent();
	}

	void setPropertiesComponent() {
		// Init view
		lblPath = new JLabel("Path: ");
		lblPath.setSize(new Dimension(50, 30));
		lblPath.setLocation(30, 20);

		btnOpenFolder = new JButton("Open");
		btnOpenFolder.setSize(new Dimension(70, 30));
		btnOpenFolder.setLocation(550, 20);
		btnOpenFolder.setActionCommand("Open");
		btnOpenFolder.setVisible(true);
		btnOpenFolder.addActionListener(this);
		
		btnClear = new JButton("Clear");
		btnClear.setSize(new Dimension(70, 30));
		btnClear.setLocation(650, 20);
		btnClear.setActionCommand("Clear");
		btnClear.setVisible(true);
		btnClear.addActionListener(this);


		tfPath = new JTextField();
		tfPath.setSize(430, 30);
		tfPath.setMargin(new Insets(3, 3, 3, 3));
		tfPath.setLocation(80, 20);
		// Panel File Input
//		mPanelFileName = new JPanel(new BorderLayout());
//		mPanelFileName.setBorder(BorderFactory.createTitledBorder("File name"));
//		mPanelFileName.setSize(new Dimension(240, 150));
//		mPanelFileName.setLocation(30, 60);
		
//		lsFileName = new JList<String>();
//		lsFileName.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
//		lsFileName.addListSelectionListener(lsFileListener);
//		scrollFileName = new JScrollPane(lsFileName,
//				ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
//				ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
//		mPanelFileName.add(scrollFileName);
		// Panel Choose Label
//		mPanelChooserLabel = new JPanel(new BorderLayout());
//		mPanelChooserLabel.setBorder(BorderFactory.createTitledBorder("Label"));
//		mPanelChooserLabel.setSize(new Dimension(240, 150));
//		mPanelChooserLabel.setLocation(280, 60);
//		
//		lsLabel = new JList<String>(LABEL);
//		lsLabel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
//		scrollLabel = new JScrollPane(lsLabel,
//				ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
//				ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
////		mPanelChooserLabel.add(scrollLabel);

		// Panel Label Result
//		mPanelLabelResult = new JPanel(new BorderLayout());
//		mPanelLabelResult.setBorder(BorderFactory.createTitledBorder("Label Result"));
//		mPanelLabelResult.setSize(new Dimension(240, 150));
//		mPanelLabelResult.setLocation(540, 60);
//
//		txtResult = new JTextArea();
//		scrollResult = new JScrollPane(txtResult,
//				ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
//				ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
//		mPanelLabelResult.add(scrollResult);
		// Panel Image Original
		mPanelImageOriginal = new JPanel(new MigLayout());
		mPanelImageOriginal.setBorder(BorderFactory.createTitledBorder("Image Original"));
		mPanelImageOriginal.setSize(new Dimension(500, 400));
		mPanelImageOriginal.setLocation(30, 100);
		
		// Panel Image Segmentation
		mPanelImageSegmentation = new JPanel(new MigLayout());
		mPanelImageSegmentation.setBorder(BorderFactory.createTitledBorder("Image Segmentation"));
		mPanelImageSegmentation.setSize(new Dimension(500, 400));
		mPanelImageSegmentation.setLocation(530, 100);
		
		// Panel Manual Label
		mPanelManualLabel = new JPanel(new BorderLayout());
		mPanelManualLabel.setBorder(BorderFactory.createTitledBorder("Manual Label"));
		mPanelManualLabel.setSize(new Dimension(200, 300));
		mPanelManualLabel.setLocation(30, 500);
		
		lsManualLabel = new JList<String>();
		lsManualLabel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		manualLabelPane = new JScrollPane(lsManualLabel,
				ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
				ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		mPanelManualLabel.add(manualLabelPane);

		// Panel Auto Label
		mPanelAutoLabel = new JPanel(new BorderLayout());
		mPanelAutoLabel.setBorder(BorderFactory.createTitledBorder("Auto Label"));
		mPanelAutoLabel.setSize(new Dimension(200, 300));
		mPanelAutoLabel.setLocation(450, 500);
		
		lsAutoLabel = new JList<String>();
		lsAutoLabel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		autoLabelPane = new JScrollPane(lsAutoLabel,
				ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
				ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		mPanelAutoLabel.add(autoLabelPane);

		
		this.add(lblPath);
		this.add(tfPath);
		this.add(btnOpenFolder);
		this.add(btnClear);
//		this.add(mPanelChooserLabel);
//		this.add(btnSet);
		this.add(mPanelImageOriginal);
		this.add(mPanelImageSegmentation);
//		this.add(mPanelFileName);
//		this.add(mPanelLabelResult);
		this.add(mPanelManualLabel);
		this.add(mPanelAutoLabel);

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		switch (e.getActionCommand()) {
		case "Open":
			dlgChooseFolder = new JFileChooser();
			dlgChooseFolder.setCurrentDirectory(new File("C:\\Users\\sev_user\\Desktop\\New_Dataset\\"));
			dlgChooseFolder.setFileSelectionMode(JFileChooser.FILES_ONLY);
			dlgChooseFolder.setAcceptAllFileFilterUsed(true);
			// create filters
			FileNameExtensionFilter dataFileFilter = new FileNameExtensionFilter("Image files (*.jpg)", "jpg");
			dlgChooseFolder.addChoosableFileFilter(dataFileFilter);
			dlgChooseFolder.setFileFilter(dataFileFilter);
			int returnVal = dlgChooseFolder.showOpenDialog(AutoAnnotation.this);
		    if(returnVal == JFileChooser.APPROVE_OPTION) {
				path = dlgChooseFolder.getSelectedFile().toString();
				System.out.println(path+"///" + getFileName(path));
				tfPath.setText(path);
				loadImage(path);
				loadSegment(getFileName(path));
		    }
			break;
		case "Set":
			// Save label of region
			File file = new File(System.getProperty("user.dir") + "\\label\\labels-boat.txt");
			try {
				BufferedWriter bw = new BufferedWriter(new FileWriter(file,true));
				String filename = lfu.arrFileName.get(lsFileName.getSelectedIndex());
//				filename = filename.replace(".jpg", "");
				bw.write(filename + "," + regionSelected + "," + Convert.switchLabel(lsLabel.getSelectedValue()) +"\n");
				bw.close();
				
				txtResult.setText("");
				FileReader reader = new FileReader(file);
                BufferedReader br = new BufferedReader(reader);
                txtResult.read( br, null );
			} catch (IOException excep) {
				excep.printStackTrace();
			}
			break;
		default:
			break;
		}
	}
	private void loadSegment(String filename){
		fImage = new FColor((BufferedImage)imgOriginal, filename, "");
		imgSegmentation = fImage.imgSeg;
		picImageSegmentLabel = new JLabel(new ImageIcon(imgSegmentation));
//		picImageSegmentLabel.addMouseListener(segmentListener);
		mPanelImageSegmentation.removeAll();
		mPanelImageSegmentation.add(picImageSegmentLabel);
		mPanelImageSegmentation.revalidate();
	}
	private String getFileName(String path){
		String filename = path.substring(path.lastIndexOf("\\")+1,path.length());
		filename = filename.replace(".jpg", "");
		return filename;
	}
	
	private void loadImage(String path){
		try {
			imgOriginal = ImageIO.read(new File(path));
			picImageOriginLabel = new JLabel(new ImageIcon((BufferedImage)imgOriginal));
			mPanelImageOriginal.removeAll();
			mPanelImageOriginal.add(picImageOriginLabel);
			mPanelImageOriginal.revalidate();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
